﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Xml.Types;

namespace OsamesFrameworkCore.Tools
{
    public static class ConfigurationTools
    {
        /// <summary>
        /// Find a value in dictionary to parse in numerics. 
        /// If the result of tryparse is false, the error code as 0
        /// The value returned is 0
        /// </summary>
        /// <param name="xmlDictionaryContainer_"></param>
        /// <param name="section_"></param>
        /// <param name="xmlNodeName_"></param>
        /// <param name="returnResultAs_"></param>
        /// <returns></returns>
        public static object FindParseXmlNodeValue(XmlDictionaryContainer xmlDictionaryContainer_, string section_, string xmlNodeName_, ReturnXmlNodeValue returnResultAs_)
        {
            string valuetoparse = null;

            try
            {
                valuetoparse = xmlDictionaryContainer_.FindValue(section_, xmlNodeName_);
            }
            catch (XmlDictionaryException ex)
            {
                LoggerService.LogToOutput(ex.Message);
            }

            // If we ask for an int without matching tag, for now we return default value for int (according to unit test).

            /*  if (string.IsNullOrEmpty(valuetoparse))
              {
                  LoggerService.LogToOutput("The value of " + section_ + "->" + xmlNodeName_ + " as null or empty");
                  return null;
              }
             * */
            switch (returnResultAs_)
            {
                case ReturnXmlNodeValue.AsInt:
                    int ireturn;
                    if (!int.TryParse(valuetoparse, out ireturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return ireturn;

                case ReturnXmlNodeValue.AsShort:
                    short sreturn;
                    if (!short.TryParse(valuetoparse, out sreturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return sreturn;

                case ReturnXmlNodeValue.AsLong:
                    long lreturn;
                    if (!long.TryParse(valuetoparse, out lreturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return lreturn;

                case ReturnXmlNodeValue.AsFloat:
                    float freturn;
                    if (!float.TryParse(valuetoparse, out freturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return freturn;

                case ReturnXmlNodeValue.AsDouble:
                    double ddreturn;
                    if (!double.TryParse(valuetoparse, out ddreturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return ddreturn;

                case ReturnXmlNodeValue.AsDecimal:
                    decimal dreturn;
                    if (!decimal.TryParse(valuetoparse, out dreturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return 0;
                    }
                    return dreturn;

                case ReturnXmlNodeValue.AsByte:
                    byte breturn;
                    if (!byte.TryParse(valuetoparse, out breturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return null;
                    }
                    return breturn;

                case ReturnXmlNodeValue.AsBool:
                    bool bbreturn;
                    if (!bool.TryParse(valuetoparse, out bbreturn))
                    {
                        LoggerService.Log(typeof(ConfigurationTools), LogLevel.Error, "Error to TryParse " + valuetoparse);
                        return false;
                    }
                    return bbreturn;

                default:
                    return null;
            }
        }

        public static string FindValue(XmlDictionaryContainer xmlDictionaryContainer_, string section_, string xmlNodeName_)
        {
            if (xmlDictionaryContainer_ == null)
                return null;
            string strReturnValue = xmlDictionaryContainer_.FindValue(section_, xmlNodeName_);

            if (string.IsNullOrEmpty(strReturnValue))
            {
                LoggerService.LogToOutput("The value of " + section_ + "->" + xmlNodeName_ + " is null or empty");
                return null;
            }

            return strReturnValue;
        }

    }
}
